package com.yixing.tech.entity.controller;

import com.yixing.tech.common.base.BaseControl;
import com.yixing.tech.common.base.BusinessException;
import com.yixing.tech.common.base.Rs;
import com.yixing.tech.entity.service.ICustomActuator;
import com.yixing.tech.entity.service.EntityBaseService;
import com.yixing.tech.entity.service.EntityAttrService;
import com.yixing.tech.entity.service.IEntityService;
import com.yixing.tech.common.vo.s5.EntitySchema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import static com.yixing.tech.common.CommonConstant.CHARSET_UTF8;

/**
 * @author yixing tech
 */
@RestController
@RequestMapping(value = "/attr")
public class AttrController extends BaseControl {
    @Resource
    private EntityAttrService attrService;
    @Autowired
    private EntityBaseService entityBaseService;
    @Resource
    ICustomActuator customActuator;
    @Resource
    IEntityService entityService;
    @Resource
    private MongoTemplate mongoTemplate;

    @PostMapping("/{objId}/insert")
    public Rs<Object> save(@PathVariable String objId, @RequestBody EntitySchema.AttrVO c) throws BusinessException {
        c.setObjId(objId);
        attrService.add(c);
        return Rs.ok();
    }

    @PostMapping("/{objId}/update/{code}")
    public Rs<Object> update(@PathVariable String objId, @PathVariable String code, @RequestBody EntitySchema.AttrVO c) throws BusinessException {
        c.setObjId(objId);
        try {
            code = code.replaceAll("%(?![0-9a-fA-F]{2})", "%25");
            code = URLDecoder.decode(code, CHARSET_UTF8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        c.setCode(code);
        attrService.update(c);
        return Rs.ok();
    }

    @DeleteMapping("/{objId}/{code}")
    public Rs<Object> delete(@PathVariable String objId, @PathVariable String code) throws BusinessException {
        try {
            code = code.replaceAll("%(?![0-9a-fA-F]{2})", "%25");
            code = URLDecoder.decode(code, CHARSET_UTF8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        attrService.delete(objId, code);
        return Rs.ok();
    }

    @DeleteMapping("/{objId}/{code}/{subCode}")
    public Rs<Object> delete(@PathVariable String objId, @PathVariable String code, @PathVariable String subCode) throws BusinessException {
        try {
            code = code.replaceAll("%(?![0-9a-fA-F]{2})", "%25");
            code = URLDecoder.decode(code, CHARSET_UTF8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        try {
            subCode = subCode.replaceAll("%(?![0-9a-fA-F]{2})", "%25");
            subCode = URLDecoder.decode(subCode, CHARSET_UTF8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        attrService.delete(objId, code, subCode);
        return Rs.ok();
    }

    @GetMapping("/types")
    public Rs<Object> getTypes() {
        return Rs.ok(attrService.getTypes());
    }

}
